/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoggedInJPanel.java
 *
 * Created on Jan 15, 2010, 12:58:50 AM
 */

package viper.GUI;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ListSelectionModel;

import viper.main.MySQLFunctions;
import viper.main.StateManager;

/**
 *
 * @author Kevin Sin
 * 
 * Revision number: 1
 * Date created: 1/17/10
 * Date of Last Back-up on SVN: none
 * 
 * <p>JPanel for when user first logs in.  Displays the peers online.
 */
public class LoggedInPanel extends javax.swing.JPanel {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** Creates new form LoggedInJPanel */
    public LoggedInPanel() {
        initComponents();
    }
    
    private javax.swing.JButton refreshButton; //Refresh button
    private javax.swing.JButton jButton1; //Logout button
    private javax.swing.JButton jButton2; //Next button
    private javax.swing.JLabel jLabel1; //Directions
    private javax.swing.JList jList1; //Displaying peers
    private javax.swing.JScrollPane jScrollPane1; //For peers list
    private DefaultListModel listModel; //Also for peers list

    /** This method is called from within the constructor to
     * initialize the form.
     */
    private void initComponents() {

    	refreshButton = new javax.swing.JButton();
    	
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();

        jLabel1 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        
        refreshButton.setText("Refresh");
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }
        });

        jButton1.setText("Log out");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        
        jLabel1.setText("Choose who you want to call:");
        
        listModel = new DefaultListModel();
    	
    	jList1 = new JList(listModel);
    	jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        jScrollPane1.setViewportView(jList1);

        jButton2.setText("Next >");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 256, Short.MAX_VALUE)
                                .addComponent(refreshButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE))))
                .addGap(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(refreshButton)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addContainerGap(44, Short.MAX_VALUE))
        );
        
    }// </editor-fold>

    /**
     * @param evt
     * Logout button pressed
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        StateManager.INSTANCE.logout();
    }

    /**
     * @param evt
     * Refresh button pressed
     */
    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	StateManager.INSTANCE.updatePeers();
    	update();
    }
    
    /**
     * @param evt
     * Next button pressed
     */
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    	//int index = jList1.getSelectedIndex();
    	String peerString = (String)jList1.getSelectedValue();
    	if(peerString!=null)
    	{
    		String peerIP = peerString.split("---")[1];
    		StateManager.INSTANCE.setupCall(peerIP);
    	}
//    	if(index!=-1)
//    		StateManager.INSTANCE.setupCall(index);
    }

    /**
     * Updates the peers to display.
     */
    public void update(){
    	listModel.clear();
    	for(Object[] peer : StateManager.INSTANCE.getPeers())
    	{
    		String peerString = peer[1] + "---" + peer[0] + "---";
    		if(peer[2].equals(MySQLFunctions.STATUS_FREE))
    			peerString+="FREE";
    		else if(peer[2].equals(MySQLFunctions.STATUS_BUSY))
    			peerString+="BUSY";
    		else if(peer[2].equals(MySQLFunctions.STATUS_BAD_RELAY))
    			peerString+="BAD RELAY";
    		listModel.addElement(peerString);
    	}
    	jList1.repaint();
    }
    
}
